package com.rich.house.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rich.common.entity.TbHouseRing;
import com.rich.common.entity.TbUser;
import com.rich.common.result.ResultData;
import com.rich.house.mapper.HouseRingMapper;
import com.rich.house.mapper.UserMapper;
import com.rich.house.service.HouseRingService;
import com.rich.house.vo.AddHoueseRingVo;
import com.rich.house.vo.SearchUidListVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @Author:fei
 * @Date:2021/8/13
 */
@Service
@Slf4j
public class HoueseRingServiceImpl implements HouseRingService {
    @Autowired
    private HouseRingMapper houseRingMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultData searchUidList(SearchUidListVo uidListVo) {
        PageHelper.startPage(uidListVo.getPage(), uidListVo.getLimit());
        List<TbHouseRing> houseRings = houseRingMapper.searchUidList(uidListVo);
        PageInfo<TbHouseRing> pageInfo = new PageInfo<>(houseRings);
        return new ResultData(pageInfo.getList(), pageInfo.getTotal());
    }

    @Override
    public boolean deleteOne(Integer id,Integer uid) {
        return houseRingMapper.deleteOne(id,uid);
    }

    @Override
    public void addHoueseRing(AddHoueseRingVo addHoueseRingVo) {
        String[] image = addHoueseRingVo.getImages();
        String imageArr= "";
        for (String s : image) {
            imageArr +=","+s;
        }
        String lastImage = imageArr.substring(1);
        TbUser tbUser = userMapper.findUid(addHoueseRingVo.getUname());
        addHoueseRingVo.setCreatTime(new Date());
        addHoueseRingVo.setIsDelect("0");
        addHoueseRingVo.setUid(tbUser.getId());
        addHoueseRingVo.setLastimage(lastImage);
        houseRingMapper.addHoueseRing(addHoueseRingVo);
    }


}
